import { BlockVolume } from "@minecraft/server";
import { Dimensions } from "@sapi-game/constants";
import { Timer } from "@sapi-game/gameComponent/common/timer/timer";
import { Vector3Utils } from "@sapi-game/utils";
import { eleCatchState } from "@/elevatorCat/catchState/state";
import { ElevatorDoorComponent } from "@/elevatorCat/mainState/components/elevatorDoor";
import { elevatorCatModule } from "@/elevatorCat/module";

export class EleloadState extends elevatorCatModule.State {
    override onEnter(): void {
        this.addComponent(Timer, {
            initialTime: 34,
            compensate: true,
        });
        this.bindTimeEvent();
    }

    bindTimeEvent() {
        const timer = this.getComponent(Timer);
        //显示倒计时
        this.eventManager.subscribe(this, timer.events.tick, (t) => {
            if (t.remainingTime > 25 || t.remainingTime <= 0) return;
            this.context.group.runCommand(`title @s title ${t.remainingTime}`);
        });

        //初始化
        this.sub(34, () => {
            this.context.group.runCommand("function elevatorCat/initPlayers");
            this.context.cat.runCommand("function elevatorCat/initCat");
            this.context.mouse.runCommand("function elevatorCat/initMouse");
            //设置默认血量
            this.context.group.forEach(
                (p) => (p.lives = this.context.config.lives)
            );
        });
        this.sub(33, () => {
            Dimensions.Overworld.runCommand(
                "fill 366 -57 77 368 -60 77 iron_bars"
            );
            Dimensions.Overworld.runCommand(
                `setblock ${Vector3Utils.toArray(this.context.bellPos).join(
                    " "
                )} ` +
                    (this.context.config.bellMode
                        ? 'bell  ["attachment"="hanging"]'
                        : "air")
            );
        });
        this.sub(30, () => {
            this.lastState?.getComponent(ElevatorDoorComponent).load();
            this.context.cat.runCommand(
                "camera @s set minecraft:free ease 4 in_quad pos 367.65 -59.00 79.58 rot 0 180"
            );
            this.context.mouse.runCommand(
                "camera @s set minecraft:free ease 4 in_quad pos 367.36 -59.00 56.50 rot 0 180"
            );
        });
        this.sub(26, () => {
            this.context.group.runCommand("camera @s clear");
        });
        this.sub(0, () => {
            this.context.mouse.title("猫猫已出动");
            this.context.mouse.runCommand("playsound mob.wither.break_block");
            try {
                Dimensions.Overworld.fillBlocks(
                    new BlockVolume(
                        { x: 366, y: -57, z: 77 },
                        { x: 368, y: -60, z: 77 }
                    ),
                    "air"
                );
            } catch (err) {}
            this.transitionTo(eleCatchState);
        });
        timer.start();
    }

    sub(time: number, func: () => void) {
        const timer = this.getComponent(Timer);
        this.eventManager.subscribe(this, timer.events.onTime, func, {
            time: time,
        });
    }
}
